<template>
  <div class="edit-div">
    <el-steps :space="400" :active="3" finish-status="success">
      <el-step title="实验信息"></el-step>
      <el-step title="实验配置"></el-step>
      <el-step title="实验详情"></el-step>
    </el-steps>
    <div class="form">
      <div class="demo-input-suffix">
        <span class="form-input-label">编辑器：</span>
        <!-- <el-radio v-model="data.detail_type" value="M" label="M">Markdown</el-radio> -->
        <el-radio v-model="data.detail_type" value="H" label="H">富文本</el-radio>
      </div>
      <div class="demo-input-suffix flex-editor">
        <span class="form-input-label"><i class="el-icon-star-on"></i>实验介绍：</span>
        <div class="inline-editor">
          <!-- <ckeditor v-if="data.detail_type == 'H'"
            :editor="editor"
            v-model="data.intro"
            :config="editorConfig"
            @ready="uploader">
          </ckeditor> -->
          <!-- <MarkdownPro v-else v-model="data.intro" /> -->
          <quill-editor 
            v-model="data.intro" 
            ref="myQuillEditor" 
            :options="editorOption">
          </quill-editor>
          <input type="file" @change="change" id="upload" style="display:none;" />
        </div>
      </div>
      <div class="demo-input-suffix flex-editor" v-for="(i, k) in chapters" :key="k">
        <span class="form-input-label"><i class="el-icon-star-on"></i>
          实验指导<span v-if="data.detail_type == 'H'">{{k + 1}}</span>：
        </span>
        <div class="inline-editor">
          <!-- <ckeditor v-if="data.detail_type == 'H'"
            :editor="editor"
            v-model="chapters[k].detail"
            :config="editorConfig"
            @ready="uploader"></ckeditor> -->
          <!-- <MarkdownPro v-else-if="data.detail_type != 'H'" v-model="chapters[k].detail" /> -->
          <quill-editor 
            v-model="chapters[k].detail" 
            ref="myQuillEditor" 
            :options="editorOption">
          </quill-editor>
          <input type="file" @change="change" id="upload" style="display:none;" />
        </div>
        <el-button type="error" @click="() => chapters.splice(k, 1)"> 删 除 </el-button>
      </div>
      <div class="demo-input-suffix flex-row-start">
        <el-button type="info" @click="addChapter"> + 添 加 实 验 指 导</el-button>
      </div>
      <div class="flex-row-end mt-4">
        <el-button type="primary" @click="onSubmit">完 成</el-button>
        <el-button @click="onCancel">上一步</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { experimentModel } from "@/api/models";
import { Message } from "element-ui";
import editorMixin from '@/mixins/editorMixin.js';
export default {
  mixins: [editorMixin],
  data() {
    return {
      data: {
        id: null,
        intro: '',
        describe: '',
        detail_type: 'H',
        describe_list: []
      },
      chapters: [],
      id: null,
    };
  },
  mounted() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id
      this.getItem()
    }
  },
  methods: {
    getItem() {
      experimentModel.item(this.id, (res) => {
        this.data = res.data;
        this.editHtml = Boolean(res.data.editor)
        if(res.data.describe_list) {
          this.chapters = res.data.describe_list
        }
      });
    },
    onCancel() {
      this.$router.go(-1);
    },
    addChapter() {
      this.chapters.push({
        detail: ""
      })
    },
    onSubmit() {
      let d = this.data;
      if (
        !d.intro ||
        !this.chapters.length
      ) {
        return Message.warning("请填完整信息");
      }
      d.describe_list = this.chapters
      d.describe = this.chapters[0].detail
      d.describe_list = this.chapters
      experimentModel.updateDesc(d, (res) => {
        Message.success(res.message);
        // Message.success("实验环境更新成功");
        this.$router.push('/experiments')
      }, (err) => {
        Message.warning(err.message);
      });
    }
  }
};
</script>
<style scoped lang="scss">
.form {
  text-align: left;
  margin-top: 50px;
}

.el-textarea {
  margin-left: 20px;
  width: 80%;
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

.el-tree-div {
  width: 50%;
  margin-left: 100px;
  border: 1px solid #cccccc;
  padding: 20px;
}

.tree-input>input {
  border: none;
  background: rgba(0, 0, 0, 0);
}

.link-konwledge {
  padding: 5px 15px;
  border: 1px solid #DCDFE6;
  margin-left: 20px;
  cursor: pointer;
}

.submit-bar {
  margin-top: 120px;
}</style>